var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_id:0,
    cart_ids:0,
    option_id:0,
    address_id:0,
    address_info:{},
    num:0,
    total_price:0,
    shipfee:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this;
    if (options) {
      if (options.goods_id){
        page.setData({
          goods_id: options.goods_id,
        })
      }
      if (options.cart_ids){
        page.setData({
          cart_ids: options.cart_ids
        })
      }
      if (options.option_id){
        page.setData({
          option_id: options.option_id,
        })
      }
      if (options.num) {
        page.setData({
          num: options.num,
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPayorderInfo();
    if (this.data.address_id>0){
      this.addressInfo();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },
  //获取支付信息
  getPayorderInfo:function(){
    var page = this;
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.shop.pay_order_info,
      method: 'POST',
      data: {
        goods_id: page.data.goods_id,
        cart_ids: page.data.cart_ids,
        option_id: page.data.option_id,
        num: page.data.num,
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            goods_info: res.data.goods,
            address_info: res.data.address,
            is_address: res.data.is_address,
            total_price:res.data.total_price,
            shipfee:res.data.shipfee
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
    
  },
  //地址信息
  addressInfo: function () {
    var page = this
    wx.showLoading({
      title: '加载中',
    })
    app.request({
      url: api.shop.address_info,
      method: 'POST',
      data: {
        id: page.data.address_id
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            address_info: res.data
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  //下单
  addOrder:function(){

    var page = this;
    if (!page.data.address_info.id){
      wx.showToast({
        title: '请设置收货地址',
        icon:'none'
      })
      return false;
    }
    wx.showLoading({
      title: '提交中',
    })
    app.request({
      url: api.shop.add_order,
      method: 'POST',
      data: {
        goods_id: page.data.goods_id,
        cart_ids: page.data.cart_ids,
        option_id: page.data.option_id,
        num: page.data.num,
        address_id: page.data.address_info.id,
      },
      success: function (res) {
        if (res.errcode == 0) {
          wx.showLoading({
            title: '获取支付',
          })
          //订单获取支付
          app.request({
            url: api.order.get_pay_data,
            method: 'POST',
            data: {
              order_id: res.data.order_id,
              pay_type: 'WECHAT_PAY'
            },
            success: function (result) {
              wx.hideLoading();
              if (result.errcode == 0) {
                var pay_data = result.data;
                wx.requestPayment({
                  timeStamp: pay_data.timeStamp,
                  nonceStr: pay_data.nonceStr,
                  package: pay_data.package,
                  signType: pay_data.signType,
                  paySign: pay_data.paySign,
                  success: function (pres) {
                    wx.showToast({
                      title: '订单支付成功',
                      icon: 'success'
                    });
                    setTimeout(function () {
                      wx.navigateTo({
                        url: '../pay-success/pay-success?order_id=' + res.data.order_id,
                      })
                    }, 1000)
                  },
                  fail: function (res) {
                    // wx.showModal({
                    //   title: '提示',
                    //   content: res,
                    //   wx
                    // })
                    console.log(res);
                    wx.showToast({
                      title: res.errMsg,
                      icon:'none'
                    });
                  }
                });
              } else {
                wx.showModal({
                  title: '提示',
                  content: result.msg,
                  showCancel: false
                });
              }
            }
          });
          
        }else{
          wx.showModal({
            title: '提示',
            content: res.errmsg,
            showCancel: false
          });
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
})